package com.huobao.yunying.fragments;

import java.util.List;

import org.simple.eventbus.EventBus;
import org.xutils.common.util.LogUtil;

import com.huobao.yunying.activitys.Act_Select_GuigeAndNum.onPriceChangedListener;
import com.huobao.yunying.entity.Changed;
import com.huobao.yunying.entity.Gouwuche_spec;
import com.huobao.yunying.entity.Guige;
import com.huobao.zhangying.R;
import com.xrz.sxm.base.dialog.BaseDialog;

import android.content.Context;
import android.os.Bundle;
import android.os.Handler;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;
import joanzapata.android.BaseAdapterHelper;
import joanzapata.android.QuickAdapter;

public class Fragment_Guige_ListView extends BaseFragment{

	private List<List<String>> list;
//	private onPriceChangedListener listener;
	private List<Gouwuche_spec> _specs;
	private String goodsid;
	private int num = 0;
	private Adapter adapter;
//	public Fragment_Guige_ListView(String goodsid,List<List<String>> list,onPriceChangedListener listener, List<Gouwuche_spec> _specs){
//		this.list = list;
//		this.goodsid = goodsid;
//		this._specs = _specs;
//
//		this.listener = listener;
//	}

	public static Fragment_Guige_ListView getInstant(Guige guige){
		Fragment_Guige_ListView instance = new Fragment_Guige_ListView();
		Bundle args = new Bundle();
		args.putSerializable("data",guige);
		instance.setArguments(args);
		return instance;
	}
	@Override
	public View onCreateContentView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
		View view = inflater.inflate(R.layout.layout_guige_listview, null);
		getIntent();
		ListView listview = (ListView) view.findViewById(R.id.listview);
		adapter = new Adapter(getActivity(), list);
		listview.setAdapter(adapter);
		new Handler().post(new Runnable() {
			
			@Override
			public void run() {
				showContentView();
			}
		});
		return view;
	}
	private void getIntent(){
		Guige guige = (Guige)getArguments().getSerializable("data");
		this.list = guige.list;
		this.goodsid = guige.goodsid;
		this._specs = guige._specs;
//		this.listener = guige.listener;
	}


	private class Adapter extends QuickAdapter<List<String>>{

		public Adapter(Context context, List<List<String>> data) {
			super(context, R.layout.item_guige_listview, data);
		}

		@Override
		protected void convert(BaseAdapterHelper arg0, final List<String> arg1) {
			int position = arg0.getPosition();
			if(position % 2 == 0){
				arg0.getView().setBackgroundResource(R.drawable.selector_listview_1);
			}else{
				arg0.getView().setBackgroundResource(R.drawable.selector_listview_2);
			}
			TextView tv_chicun = arg0.getView(R.id.tv_chicun);
			TextView tv_jiage = arg0.getView(R.id.tv_jiage);
			TextView  tv_jianshu = arg0.getView(R.id.tv_jianshu);
			tv_chicun.setText(arg1.get(0));
			tv_jiage.setText("￥"+arg1.get(1));
			tv_jianshu.setText(arg1.get(2)+"件");
			TextView tv_jian = arg0.getView(R.id.tv_jian);
			TextView tv_jia = arg0.getView(R.id.tv_jia);
			final TextView tv_number = arg0.getView(R.id.tv_number);
			checkNum(tv_number, arg1.get(3));
			tv_jian.setOnClickListener(new OnClickListener() {
				
				@Override
				public void onClick(View v) {
					int num = Integer.parseInt(tv_number.getText().toString().trim());
					if(num == 0){
						return;
					}
					num --;
					tv_number.setText(String.valueOf(num) );
					sendChange(-Float.parseFloat(arg1.get(1)), -1, arg1.get(3));
				}
			});
			tv_jia.setOnClickListener(new OnClickListener() {
				
				@Override
				public void onClick(View v) {
					int num = Integer.parseInt(tv_number.getText().toString().trim());
					if(goodsid != null && goodsid.equals("2") && num>=1){
						Toast.makeText(getActivity(), "此商品只能进货一册", Toast.LENGTH_SHORT).show();
						return;
					}
					num ++;
					tv_number.setText(String.valueOf(num) );
					sendChange(Float.parseFloat(arg1.get(1)), 1,arg1.get(3));
				}
			});
			tv_number.setOnClickListener(new OnClickListener() {
				@Override
				public void onClick(View view) {
					if(TextUtils.isEmpty(arg1.get(2))){
						Toast.makeText(context, "此商品没有库存", Toast.LENGTH_SHORT).show();
						return;
					}
					final int oldNum = Integer.parseInt(tv_number.getText().toString().trim());
					BaseDialog.showEditTextDialog(context, oldNum, Integer.parseInt(arg1.get(2)), new BaseDialog.OnDialogSelectListener() {
						@Override
						public void onSelect(String input) {
							int num = Integer.parseInt(input);
							tv_number.setText(num+"");
							sendChange(Float.parseFloat(arg1.get(1)), num- oldNum,arg1.get(3));
						}
					});
				}
			});
		}
		
	}

	private void sendChange(float change, int numChange, String type){
		Changed ch = new Changed();
		ch.setChange(change);
		ch.setNumChange(numChange);
		ch.setType(type);
		EventBus.getDefault().post(ch, "onPriceChanged");
	}
	
	private void checkNum(TextView tv_number, String id){
		if(_specs != null && num != -123){
			for(Gouwuche_spec _spec : _specs){
				if(_spec.spec_id.equals(id)){
					tv_number.setText(_spec.num+"");
					return;
				}
			}
		}
	}
}
